<template>
  <div v-show="$route.name == 'material-purchase'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>原料采购单</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row class="query">
        <el-col :span="24">
          <el-input placeholder="请填写采购单号"
            v-model="houseNumber"
            clearable style="width: 200px;margin-left: 5px;margin-bottom: 10px;">
          </el-input>
          <el-button type="primary" @click="search" style="margin-left: 5px">查询</el-button>
          <el-button style="margin-left: 5px"  @click="reset">重置</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-button type="success" @click="addMaterialMul">批量采购</el-button>
        </el-col>
      </el-row>
      <el-table
        border
        v-loading="loading"
        :data="materialInData"
        style="margin-top: 10px;">
        <el-table-column label="日期" prop="default_time" min-width="110" align="center" fixed="left">
          <template slot-scope="scope">
            <span>{{ scope.row.default_time ? new Date(scope.row.default_time).toLocaleDateString().split('/').join('-') : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="采购单号" prop="house_in_number" min-width="110" align="center" ></el-table-column>
        <el-table-column label="采购状态" prop="status" min-width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1">已完成</span>
            <span v-else-if="scope.row.status === 2">审核中</span>
            <span v-else-if="scope.row.status === 3">驳回</span>
          </template>
        </el-table-column>
        <el-table-column label="创建日期" prop="insert_time" min-width="110" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.insert_time ? new Date(scope.row.insert_time).toLocaleDateString().split('/').join('-') : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="clickButton(scope.row.id, 1)" style="margin-right: 5px;">详情</el-button>
            <el-button size="mini" type="warning" @click="clickButton(scope.row.id, 2)" v-if="scope.row.status === 2 || scope.row.status === 3">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.currPage"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      houseNumber: '',
      materialInData: []
    }
  },
  mounted() {
    this.getHouseInList()
  },
  methods: {
    async getHouseInList() {
      this.loading = true
      const params = {
        page: this.pager.currPage,
        pageSize: this.pager.pageSize,
        houseNumber: this.houseNumber,
        status: [1, 2, 3],
        process: 0
      }
      const { data: result } = await this.$http.get('/house-in', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.materialInData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.materialInData = result.data
      this.pager = result.pager
    },
    search() {
      this.pager.currPage = 1
      this.getHouseInList()
    },
    reset() {
      this.houseNumber = ''
      this.materialInData = []
      this.pager.currPage = 1
      this.getHouseInList()
    },
    clickButton(id, type) {
      if (type === 1) {
        this.$router.push({
          name: 'material-purchase-detail',
          params: { orderId: id, come: 'material-purchase' }
        })
      } else if (type === 2) {
        this.$router.push({
          name: 'material-purchase-update',
          params: { orderId: id, come: 'material-purchase' }
        })
      }
    },
    addMaterialMul() {
      this.$router.push({ 'name': 'material-multi' })
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getHouseInList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getHouseInList()
    },
  }
}
</script>